package com.ai.leetcode;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Code002_ThreadLocal {
    static int a = 0 ;
    static {
        a = 1;
        b = 1;
    }
    static int b = 0;

    private B clzB = new B();

    static class LocalVariable {
        private Long[] a = new Long[1024 * 1024];
    }

    // (1)
    final static ThreadPoolExecutor poolExecutor
            = new ThreadPoolExecutor(5, 5, 1, TimeUnit.MINUTES,
            new LinkedBlockingQueue<>());
    // (2)
    final static ThreadLocal<LocalVariable> localVariable = new ThreadLocal<>();

    public static void main(String[] args) throws Exception {
        Code002_ThreadLocal a = new Code002_ThreadLocal();
        long num = 4321 ;

        long ret = a.clzB.test(num);

        System.out.println(ret);

        // (3)
        Thread.sleep(5000 * 4);
        for (int i = 0; i < 50; ++i) {
            poolExecutor.execute(new Runnable() {
                public void run() {
                    // (4)
                    localVariable.set(new LocalVariable());
                    // (5)
                    System.out.println("use local varaible" + localVariable.get());
                    localVariable.remove();
                }
            });
        }
        // (6)
        System.out.println("pool execute over");
    }

    static class B {
        private int a = 1234;

        static long C = 1111;

        public long test(long num) {
            long ret = this.a + num + C;
            return ret;
        }
    }


}
